<template>
  <div class="app-container">
    <el-dialog :title="title" :visible.sync="dialogFormVisible" width="800px">
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
      >
        <FormItems
          dialog-status="update"
          :form-items="formItems"
          :temp="temp"
          :configs="configs"
        />
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">
          确认
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import odoo from '@/odooapi'

import { parseTime } from '@/utils'
import FormItems from '@/selfComponents/FormDialog/FormItems'

export default {
  name: 'FormDialog',
  components: { FormItems },
  directives: {},
  filters: {},
  props: {
    modelName: { type: String, default: '' }
  },
  data() {
    return {
      Model: undefined,
      wizardModel: '',
      title: 'ReportWizard',
      dialogFormVisible: true,
      formItems: [],
      temp: {},
      rules: {
        // type: [{ required: true, message: 'type is required', trigger: 'change' }],
        // timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        // title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      configs: {}
    }
  },

  async created() {
    this.Model = odoo.env(this.modelName)
    this.set_wizard()
  },

  methods: {
    async set_wizard() {
      const configs_wizard = this.Model.configs.wizard
      const { name, fields_default, formItems, context } = configs_wizard

      this.wizardModel = name
      this.wizardFields = fields_default

      this.formItems = await this.Model.env(name).formOptions(formItems)

      const tempData = formItems.reduce(
        (acc, cur) => {
          acc[cur.name] = cur.default
          return acc
        },
        { id: undefined }
      )

      this.temp = tempData
      const wizard_rec = await this.Model.report_wizard_create({
        model: name,
        fields: fields_default,
        context
      })

      this.temp = Object.keys(this.temp).reduce((acc, cur) => {
        acc[cur] = wizard_rec[cur]
        return acc
      }, {})
    },

    get_values() {
      const tempData = Object.assign({}, this.temp)
      this.formItems.forEach(item => {
        if (item.type === 'date') {
          const date = tempData[item.name]
          if (date) {
            tempData[item.name] = parseTime(date, '{y}-{m}-{d}')
          }
        } else if (item.type === 'date_month') {
          const date_month = tempData[item.name]
          if (date_month) {
            const new_date = `${parseTime(date_month, '{y}-{m}')}-01`
            tempData[item.name] = new_date
          }
        } else if (item.type === 'select' && !item.multiple) {
          const val = tempData[item.name]
          if (!val) {
            tempData[item.name] = null
          }
        } else if (item.type === 'select' && item.multiple) {
          tempData[item.name] = [[6, 0, tempData[item.name]]]
        }
      })

      return tempData
    },

    submit() {
      this.$refs['dataForm'].validate(async valid => {
        if (valid) {
          const tempData = this.get_values()
          console.log('xxxxxxx, report form', tempData)

          const res = await this.Model.report_print({
            model: this.wizardModel,
            values: tempData,
            fields: this.wizardFields
          })
          // console.log('xxxxxxx, report form 2', res)

          if (res) {
            this.$emit('changed', res)
            this.dialogFormVisible = false
            this.$notify({
              title: '成功',
              message: '获取报表数据ok',
              type: 'success',
              duration: 2000
            })
          }
        }
      })
    }
  }
}
</script>
